//
//  HMVisitorViewController.swift
//  GuoGuo
//
//  Created by 艾小新 on 16/8/2.
//  Copyright © 2016年 xiaoxin. All rights reserved.
//

import UIKit
import SVProgressHUD

class HMVisitorViewController: UITableViewController {
    
    var isLogon: Bool = HMUserLoginViewModel.sharedUserLoginViewModel.userLogin

    /// 在控制器没有创建出View的时候会调用
    override func loadView() {

        if isLogon {
            // 用户登录,直接显示tableView就可以了
            super.loadView()
        }else {
            // 如果没有登录,那么就登录我们的访客视图
            setUpVisitorView()
        }
    }

    func setUpVisitorView() {
        
        view = visitView
        
        // 设置右左的登录按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", target: self, action: #selector(HMVisitorViewController.login))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", target: self, action: #selector(HMVisitorViewController.login))
    }
    
    @objc private func login(){
        // model
        presentViewController(UINavigationController(rootViewController: HMOauthViewController()), animated: true, completion: nil)
   
    }
    
    
    // MARK: 懒加载访客视图
    lazy var visitView: HMVisitorView = {
        let v = HMVisitorView()
        v.delegate = self
        v.loginCallBack = {
            self.login()
        }
        return v
    }()
}

extension HMVisitorViewController: HMVisitorViewDelegate {
    func visitorView(visitorView: HMVisitorView, loginButtonDidCliked: UIButton) {
        // 执行登录操作
        login()
    }
}
